package cn.ld.fj.web.desk;

import cn.ld.fj.entity.DeskShop;
import cn.ld.fj.entity.DeskShopOrderInfo;
import cn.ld.fj.service.desk.DeskShopManager;
import cn.ld.fj.service.desk.DeskShopOrderInfoManager;
import cn.ld.fj.service.log.OperationLogManager;
import cn.ld.fj.util.DwzUtil;
import cn.ld.fj.web.JsonActionSupport;
import com.google.common.collect.Lists;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import net.esoar.modules.utils.web.struts2.Struts2Utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 用户管理Action.
 * <p/>
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 *
 * @author fly
 */
// 定义URL映射对应/account/user.action
@Namespace("/deskShop")
// 定义名为reload的result重定向到user.action, 其他result则按照convention默认.
@Results({@Result(name = JsonActionSupport.RELOAD, location = "desk-shop-order-info.action", type = "redirect")})
public class DeskShopOrderInfoAction extends JsonActionSupport<DeskShopOrderInfo> {


    @Autowired
    private DeskShopManager deskShopManager;

    @Autowired
    private OperationLogManager operationLogManager;

    @Autowired
    private DeskShopOrderInfoManager deskShopOrderInfoManager;

    private Long id;
    private DeskShopOrderInfo entity;
    private Page<DeskShopOrderInfo> page = new Page<DeskShopOrderInfo>(10);


    private List<DeskShop> deskShops = Lists.newArrayList();

    public List<DeskShop> getDeskShops() {
        return deskShops;
    }

    public void setDeskShops(List<DeskShop> deskShops) {
        this.deskShops = deskShops;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setEntity(DeskShopOrderInfo entity) {
        this.entity = entity;
    }

    public void setPage(Page<DeskShopOrderInfo> page) {
        this.page = page;
    }

    public Long getId() {
        return id;
    }

    public DeskShopOrderInfo getEntity() {
        return entity;
    }

    public Page<DeskShopOrderInfo> getPage() {
        return page;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = deskShopOrderInfoManager.getOne(id);
        } else {
            entity = new DeskShopOrderInfo();
        }
    }

    // -- CRUD Action 函数 --//
    @Override
    public String list() throws Exception {
        List<PropertyFilter> filters = PropertyFilter
                .buildFromHttpRequest(Struts2Utils.getRequest());
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }

        page = deskShopOrderInfoManager.getPage(page, filters);
        if(CollectionUtils.isNotEmpty(page.getResult())){
            for(DeskShopOrderInfo deskShopOrderInfo : page.getResult()){
                DeskShop deskShop = deskShopManager.getOne(deskShopOrderInfo.getDeskShopId());
                if(deskShop != null){
                    deskShopOrderInfo.setDnumber(deskShop.getDnumber());
                }
            }
        }

        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        deskShops = deskShopManager.findAll();
        return INPUT;
    }

    @Override
    public void save() throws Exception {

        entity.setCreateDate(new Date());
        deskShopOrderInfoManager.save(entity);
        operationLogManager.saveLog("增加餐桌预定信息");

        Struts2Utils.renderHtml(DwzUtil.getCloseCurrentReturn("w_deskShopOrderInfo",
                "操作成功"));
    }


    @Override
    public void delete() throws Exception {


        try {
            deskShopOrderInfoManager.delete(id);
            operationLogManager.saveLog("删除餐桌预定信息");

        } catch (Exception e) {
            return;
        }

        Struts2Utils.renderHtml(DwzUtil.getNavtabReturn("w_deskShopOrderInfo",
                "操作成功"));
    }

    @Override
    public DeskShopOrderInfo getModel() {
        return entity;
    }
}
